//
//  LPPRotateWheelView.m
//  Lottery
//
//  Created by 刘盼 on 16/1/24.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPRotateWheelView.h"

#define KBtnCount 12

@interface LPPRotateWheelView()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *rotateView;

@property(nonatomic,strong)UIButton *currentBtn;

@property(nonatomic,strong)CADisplayLink *link;

@end
@implementation LPPRotateWheelView

+(instancetype)rotateWheelView {
    
    return [[[NSBundle mainBundle]loadNibNamed:@"LPPRotateWheelView" owner:nil options:nil]lastObject];
}

-(void)awakeFromNib {
   
    for (int i = 0; i < KBtnCount; i++) {
        
        UIButton *btn = [[UIButton alloc]init];
        
        UIImage *image = [UIImage imageNamed:@"LuckyAstrology"];
        
        UIImage *imagePress = [UIImage imageNamed:@"LuckyAstrologyPressed"];
        
        //裁剪过后的小图片
        image = [self clipImage:image withIndex:i];
        
        imagePress = [ self clipImage:imagePress withIndex:i];
        
        [btn setImage:image forState:UIControlStateNormal];
        
        [btn setImage:imagePress forState:UIControlStateSelected];
        //设置字的位置
        [btn setImageEdgeInsets:UIEdgeInsetsMake(-52, 0, 0, 0)];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.rotateView addSubview:btn];
    }
}
#pragma mark---------按钮点击事件-----------
//开始选号按钮点击
- (IBAction)startRotateClick:(id)sender {
  
    if (![self.rotateView.layer animationForKey:@"key"]) {
        
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"key"];
        anim.keyPath = @"transform.rotation";
        
        anim.toValue = @(2 *M_PI *5 - self.currentBtn.tag * 2 * M_PI / 12);
        
        anim.duration = 3;
        //不希望回到原来位置
        anim.fillMode = kCAFillModeForwards;
        anim.removedOnCompletion = NO;
        
        [self.rotateView.layer addAnimation:anim forKey:@"key"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(anim.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.rotateView.transform = CGAffineTransformMakeRotation([anim.toValue floatValue]);
            
            [self.rotateView.layer removeAnimationForKey:@"key"];
            
            self.link.paused = YES;
            
//            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的幸运号码是11111" preferredStyle:UIAlertControllerStyleAlert];
//            
//            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//               
//                self.link.paused = NO;
//            }]];
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的幸运号码是11111"  delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        });
    }
}
-(void)BtnClick:(UIButton *)sender{
    
    self.currentBtn.selected = NO;
    
    sender.selected = YES;
    
    self.currentBtn = sender;
    
}

#pragma mark--------<UIAlertViewDelegate>---------
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    self.link.paused = NO;
}
#pragma mark---------程序一运行就转动------------

-(void)startRotate {
    
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];
    
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
//转动
-(void)rotate {
    
    self.rotateView.transform = CGAffineTransformRotate(self.rotateView.transform, 2 * M_PI / 60 / 5);
}
#pragma mark------------剪切图片----------------

-(UIImage *)clipImage:(UIImage *)image withIndex:(int)index {
    
    CGFloat w = image.size.width / KBtnCount * [UIScreen mainScreen].scale;
    
    CGFloat h = image.size.height * [UIScreen mainScreen].scale;
    
    CGFloat x = w * index;
    
    CGFloat y = 0;
    
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(x, y, w, h));
    
    return [UIImage imageWithCGImage:imageRef scale:2.3 orientation:UIImageOrientationUp];
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    for (int i = 0; i < self.rotateView.subviews.count; i++) {
        CGFloat w = 68;
        CGFloat h = 143;
        CGFloat x = 0;
        CGFloat y = 0;
        
        UIButton *btn = self.rotateView.subviews[i];
        
        btn.frame = CGRectMake(x, y, w, h);
        
        CGFloat centerX = self.bounds.size.width *0.5;
        
        CGFloat centerY = self.bounds.size.height *0.5;
        
        btn.center = CGPointMake(centerX, centerY);
        //锚点
        btn.layer.anchorPoint = CGPointMake(0.5, 1);
        
        CGFloat angle = 2 *M_PI / KBtnCount;
        
        btn.transform = CGAffineTransformMakeRotation(angle *i);
        
    }
}
@end
